package ru.silviasaint.fridge.client.widgets.component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import ru.silviasaint.fridge.shared.bean.ProductSkinny;
import ru.silviasaint.fridge.shared.ingredients.SearchAction;
import ru.silviasaint.fridge.shared.ingredients.SearchResult;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwtplatform.dispatch.client.DefaultDispatchAsync;
import com.gwtplatform.dispatch.client.DefaultExceptionHandler;
import com.gwtplatform.dispatch.client.DefaultSecurityCookieAccessor;
import com.gwtplatform.dispatch.client.actionhandler.DefaultClientActionHandlerRegistry;
import com.gwtplatform.dispatch.shared.DispatchAsync;

public class IngredientSearcher extends Composite implements HasValue<List<ProductSkinny>> {

	private static IngredientSearcherUiBinder uiBinder = GWT
			.create(IngredientSearcherUiBinder.class);

	private final DispatchAsync dispatcher = new DefaultDispatchAsync(
			new DefaultExceptionHandler(), new DefaultSecurityCookieAccessor(),
			new DefaultClientActionHandlerRegistry());

	interface IngredientSearcherUiBinder extends
			UiBinder<Widget, IngredientSearcher> {
	}

	@UiField
	VerticalPanel ingredients;
	@UiField(provided = true)
	SuggestBox text;
	List<ProductSkinny> values = new ArrayList<ProductSkinny>();
	HashMap<Long, HorizontalPanel> panels = new HashMap<Long, HorizontalPanel>();
	HashMap<String, ProductSkinny> ids = new HashMap<String, ProductSkinny>();

	public IngredientSearcher() {
		final ImageSuggest oracle = new ImageSuggest();
		text = new AddNewSuggestBox(oracle);

		text.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (text.getValue().length() > 3) {

					SearchAction action = new SearchAction();
					action.setExclude(values);
					action.setName(text.getValue());
					dispatcher.execute(action,
							new AsyncCallback<SearchResult>() {

								@Override
								public void onFailure(Throwable paramThrowable) {
									Window.alert("internal error");
									GWT.log(paramThrowable.getMessage());
								}

								@Override
								public void onSuccess(SearchResult result) {
									for (ProductSkinny product : result
											.getResult()) {
										oracle.add(product);
										ids.put(product.getValue(), product);
									}
								}

							});

				}

			}
		});
		text.addValueChangeHandler(new ValueChangeHandler<String>() {
			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				addIngredient(event.getValue());
			}

		});
		initWidget(uiBinder.createAndBindUi(this));

	}

	void addIngredient(String ingredient) {
		final ProductSkinny product = ids.get(ingredient);
		if (product == null){
			DialogBox db = new DialogBox(true,true);
			db.setText("Ingredient Not Found");
			DockPanel  dp = new DockPanel ();
			dp.add(new HTML("DICK"), DockPanel.NORTH);
			dp.add(new Button("OK"), DockPanel.SOUTH);
			dp.add(new Button("close"), DockPanel.SOUTH);
			db.setWidget(dp);
			RootPanel.get().add(db);
		}
		HorizontalPanel panel = new HorizontalPanel();

		values.add(product);
		panel.add(new HTML(product.getValue()));
		panel.add(new TextBox());
		panel.add(new ListBox());
		Button deleteButton = new Button("delete");
		deleteButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				deleteIngredient(product.getId());
			}

		});
		panel.add(deleteButton);
		panels.put(product.getId(), panel);

		ingredients.add(panel);
	}

	void deleteIngredient(Long ingredient) {
		ingredients.remove(panels.get(ingredient));
		panels.remove(ingredient);
		values.remove(ingredient);

	}


	@Override
	public HandlerRegistration addValueChangeHandler(
			ValueChangeHandler<List<ProductSkinny>> paramValueChangeHandler) {
		return null;
	}

	@Override
	public List<ProductSkinny> getValue() {
		return null;
	}

	@Override
	public void setValue(List<ProductSkinny> value) {

	}

	@Override
	public void setValue(List<ProductSkinny> value, boolean paramBoolean) {
		// TODO Auto-generated method stub

	}

}
